package in.muton.edushare.processing.objects;

import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.wicket.model.IModel;

import com.edushare.common.vo.CourseDeptLink;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Enrolments;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.UniversityCourseLinks;
import com.edushare.common.vo.Users;

public class CourseDataHandler implements Serializable {

	private static final long serialVersionUID = -8961203960778524069L;

	public CourseDataHandler(IModel<CourseInstances> c) {
		cInstance = c;
		course = new CayenneModel<Courses>(c.getObject().getCourse());
	}

	public CourseDataHandler(IModel<Courses> c, int dummy) {
		course = c;
		cInstance = null;
	}

	public CayenneList<Users> getPeople() {
		List<Users> cL = new ArrayList<Users>();
		List<Enrolments> eR = cInstance.getObject().getEnrolmentList();
		for (Enrolments e : eR) {
			cL.add(e.getUser());
		}
		Collections.sort(cL);
		return new CayenneList<Users>(cL);
	}
	
	public CayenneList<Users> getPeopleByRole(int roles){
		List<Users> cL = new ArrayList<Users>();
		List<Enrolments> eR = cInstance.getObject().getEnrolmentList();
		for (Enrolments e : eR) {
			if (e.getEnrolmentType() == roles) {
				cL.add(e.getUser());
			}
		}
		Collections.sort(cL);
		return new CayenneList<Users>(cL);		
	}

	public CayenneList<Users> getTA() {
		return getPeopleByRole(Enrolments.TEACHING_ASSISTANT);
	}

	public CayenneList<Users> getTeachers() {
		CayenneList<Users> u1 = getPeopleByRole(Enrolments.TEACHER);
		u1.add(getPeopleByRole(Enrolments.TEACHING_ASSISTANT));
		return u1;
	}

	public CayenneList<Users> getStudents() {
		CayenneList<Users> u1 = getPeopleByRole(Enrolments.STUDENT);
		u1.add(getPeopleByRole(Enrolments.OBSERVER));
		return u1;
	}

	public IModel<Departments> getOwner() {
		List<CourseDeptLink> cd = course.getObject().getDeptLinks();
		for (CourseDeptLink c : cd) {
			if (c.getCourseDeptAssnType() == CourseDeptLink.DEPT_COURSE_OWNER) {
				return new CayenneModel<Departments>(c.getDepartment());
			}
		}
		return null;
	}

	public IModel<Universities> getOwnerUniv() {
		List<UniversityCourseLinks> cd = course.getObject().getUniversities();
		for (UniversityCourseLinks c : cd) {
			if (c.getUnivCourseAssnType() == UniversityCourseLinks.UNIV_COURSE_OWNER) {
				return new CayenneModel<Universities>(c.getUniversity());
			}
		}
		return null;
	}

	public CayenneList<Universities> getUnivs() {
		List<UniversityCourseLinks> cd = course.getObject().getUniversities();
		CayenneList<Universities> univs = new CayenneList<Universities>(null);
		for (UniversityCourseLinks c : cd) {
			univs.add(c.getUniversity());
		}
		return univs;
	}

	public CayenneList<Departments> getDept() {
		List<CourseDeptLink> cd = course.getObject().getDeptLinks();
		CayenneList<Departments> cL = new CayenneList<Departments>(null);
		for (CourseDeptLink c : cd) {
			cL.add(c.getDepartment());
		}
		return cL;
	}

	IModel<CourseInstances> cInstance;
	IModel<Courses> course;

	public String getDeptRole(Users loggedInUser) {
		CayenneList<Users> univAdmins = new UnivDataHandler(getOwnerUniv())
				.getAdmins();
		if (univAdmins.contains(loggedInUser))
			return "University Administrator";

		CayenneList<Users> deptAdmins = new DeptDataHandler(getOwner())
				.getPrimaryAdmins();
		if (deptAdmins.contains(loggedInUser))
			return "Department Administrator";

		List<Enrolments> uD = loggedInUser.getEnrolments();
		for (Enrolments u : uD) {
			if (u.getCourseInstance() == cInstance.getObject()) {
				if (u.getEnrolmentType() == Enrolments.TEACHER)
					return "Faculty";
				else if (u.getEnrolmentType() == Enrolments.TEACHING_ASSISTANT)
					return "Teaching Assistant";
				else if (u.getEnrolmentType() == Enrolments.STUDENT)
					return "Student";
				else if (u.getEnrolmentType() == Enrolments.OBSERVER)
					return "Audit Student";
			}
		}

		return "Visitor";
	}
}
